<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="风险标题" prop="riskTitle">
        <el-input
          v-model="queryParams.riskTitle"
          placeholder="请输入项目风险标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目" prop="projectId">
        <el-select v-model="queryParams.projectId"
                   placeholder="请输入项目主键"
                   clearable
                   @keyup.enter.native="handleQuery">
          <el-option v-for="item in projectList" :key="item.projectId" :label="item.projectName" :value="item.projectId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:projectRisk:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:projectRisk:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:projectRisk:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:projectRisk:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectRiskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="风险类别名称" align="center" prop="risktypeName" />
      <el-table-column label="风险标题" align="center" prop="riskTitle" />
      <el-table-column label="跟进人" align="center" prop="nickName" />
      <el-table-column label="状态" align="center" prop="riskStatus" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.riskStatus == 1" type="success">待跟进</el-tag>
          <el-tag v-if="scope.row.riskStatus == 2" type="warning">跟进中</el-tag>
          <el-tag v-if="scope.row.riskStatus == 3" type="danger">已解决</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="风险内容" align="center" prop="riskContent" >
        <template slot-scope="scope">
          <span>{{ scope.row.riskContent !== null ? scope.row.riskContent.length > 10 ? scope.row.riskContent.substring(0, 10) + '...' : scope.row.riskContent : '' }}</span>
          <el-popover
            placement="top-start"
            title="详情"
            width="200"
            trigger="hover"
            v-if="scope.row.riskContent.length > 10">
            <span>{{scope.row.riskContent}}</span>
            <el-button slot="reference" type="text" style="font-size: 16px;padding: 5px">详情</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="所属项目" align="center" prop="projectName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:projectRisk:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:projectRisk:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目风险对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目" prop="projectId">
          <el-select v-model="form.projectId"
                     placeholder="请输入项目主键"
                     clearable>
            <el-option v-for="item in projectList" :key="item.projectId" :label="item.projectName" :value="item.projectId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="风险类别" prop="risktypeId">
          <el-select v-model="form.risktypeId"
                     placeholder="请输入风险类别"
                     clearable>
            <el-option v-for="item in riskTypeList" :key="item.risktypeId" :label="item.risktypeName" :value="item.risktypeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="风险标题" prop="riskTitle">
          <el-input v-model="form.riskTitle" placeholder="请输入项目风险标题" />
        </el-form-item>
        <el-form-item label="跟进人" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入跟进人" readonly disabled>
            <template slot="append">
              <el-button type="primary" @click="selectUser">选择</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="风险内容">
          <el-radio-group v-model="form.riskStatus">
            <el-radio :label="1">待跟进</el-radio>
            <el-radio :label="2">跟进中</el-radio>
            <el-radio :label="3">已解决</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="风险内容">
          <el-input v-model="form.riskContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--  选择跟进人的dialog-->
    <el-dialog ref="userDialog" title="选择跟进人" width="500px" :visible.sync="openUser" append-to-body>
      <project-dept-user-tree-selector
        :project-id="form.projectId"
        :showCheckbox=false
        @handleNodeClick="handleNodeClick"
      />
    </el-dialog>

  </div>
</template>

<script>
import { listProjectRisk, getProjectRisk, delProjectRisk, addProjectRisk, updateProjectRisk } from "@/api/project/projectRisk";
import {listProjectByUserId} from "@/api/project/project";
import {listRiskType} from "@/api/project/riskType";
import ProjectDeptUserTreeSelector from '@/components/ProjectDeptUserTreeSelector'
import Log from "@/views/monitor/job/log.vue";
export default {
  name: "ProjectRisk",
  components: {
    ProjectDeptUserTreeSelector
  },
  data() {
    return {
      // 跟进人dialog显隐
      openUser:false,
      // 风险列表的数组
      riskTypeList: [],
      // 项目列表的数组
      projectList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目风险表格数据
      projectRiskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        riskTitle: null,
      },
      // 表单参数
      form: {
        projectId: null,
        risktypeId: null,
        riskTitle: null,
        userId: null,
        riskStatus: null,
        riskContent: null
      },
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "项目不能为空", trigger: "blur" }
        ],
        risktypeId: [
          { required: true, message: "风险类别不能为空", trigger: "blur" }
        ],
        riskTitle: [
          { required: true, message: "风险标题不能为空", trigger: "blur" },
          { min: 0, max: 36, message: "长度在 0 到 36 个字符", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "跟进人不能为空", trigger: "blur" }
        ],
        riskStatus: [
          { required: true, message: "风险状态不能为空", trigger: "blur" }
        ],
        riskContent: [
          { required: true, message: "风险内容不能为空", trigger: "blur" },
          { min: 0, max: 200, message: "长度在 0 到 200 个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getListProject();
    this.getListRiskType();
  },
  mounted() {
    this.$refs.userDialog.rendered=true
  },
  computed:{
    /**
     * 获取projectId
     * @returns {string|*}
     */
    getUserId() {
      return this.form.projectId;
    },
  },
  watch: {
    //监听 form.projectId变化时，清空userId和nickName
    getUserId: function (val) {
      if (!this.form.riskId){
        console.log('form.projectId',val)
        this.form.userId = null;
        this.form.nickName = null;
      }

    }
  },
  methods: {
    selectUser() {
      this.openUser = true;
      console.log('form.userId',this.form.userId)
    },
    handleNodeClick(data) {
      if (data.level == 4) {
        this.form.userId = data.id;
        this.form.nickName = data.label;
        this.openUser = false;
      }else {
        this.$message.error('不能选择公司,请选择用户');
      }
    },
    /** 用户能看见的项目列表 */
    getListProject() {
      listProjectByUserId().then(response => {
        this.projectList = response.data;
      });
    },
    /** 查询风险类别列表 */
    getListRiskType() {
      listRiskType().then(response => {
        this.riskTypeList = response.rows;
      });
    },
    /** 查询项目风险列表 */
    getList() {
      this.loading = true;
      listProjectRisk(this.queryParams).then(response => {
        this.projectRiskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        riskId: null,
        projectId: null,
        risktypeId: null,
        riskTitle: null,
        userId: null,
        riskStatus: 1,
        riskContent: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.riskId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目风险";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const riskId = row.riskId || this.ids
      getProjectRisk(riskId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目风险";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.riskId != null) {
            updateProjectRisk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectRisk(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const riskIds = row.riskId || this.ids;
      this.$modal.confirm('是否确认删除项目风险编号为"' + riskIds + '"的数据项？').then(function() {
        return delProjectRisk(riskIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/projectRisk/export', {
        ...this.queryParams
      }, `projectRisk_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
</style>
